<?php

if (!defined('DIR_SEP'))
    define('DIR_SEP', DIRECTORY_SEPARATOR);

if (!defined('MODULES_DIR'))
    define('MODULES_DIR', dirname(__FILE__).DIR_SEP."..".DIR_SEP);

require_once(MODULES_DIR."IManager.inc");
require_once(MODULES_DIR."log.inc");
require_once(MODULES_DIR."config.inc");
require_once(MODULES_DIR."util.inc");
require_once(MODULES_DIR."tag.inc");

require_once("blogpost.inc");

class BlogManager implements IManager
{
	public function getAdminMenu()
	{
		//get admin menu should be deprecated, and managed in get admin menu.
		//should be managed in settings page, add tab for blog settings
		global $G_HOME;
		return "<li class='navCategory'>Blog management</li>
			<li class='navItem'><a href='".Util::combinePath($G_HOME,"index.php?pg=add_post")."'>Add post</a></li>";
	}
	
	public function getMenu()
	{
		global $G_HOME;

		$menuTitle = Config::getInstance()->get('/site/features/blog/menu/user/@title');
		
		$menu = "";
		
		if(isset($menuTitle) && strlen($menuTitle) > 0)
			$menu .= "<li class='navCategory'>".$menuTitle."</li>";
		
		$years = BlogPost::getTitles();
		
		$menu .= "<ul class='archiveYears'>";
		foreach($years as $year)
		{
			$menu .= "<li class='archiveYear'><a href='javascript:void 0;'>".$year->title."</a><ul class='archiveMonths'>";
			
			foreach($year->children as $month)
			{
				$menu .= "<li class='archiveMonth'><a href='javascript:void 0;'>".$month->title."</a><ul class='archivePosts'>";
				
				foreach($month->children as $post)
				{
					$menu .="<li class='archivePost'><a href='".$post->getUrl()."'>".$post->title."</a></li>";
				}
				$menu .= "</ul></li>";
			}
			$menu .= "</ul></li>";
		}

		$menu .= "</ul>";
		$menu .= 		
			"<script>
				$('ul.archiveMonths,ul.archivePosts').hide();
				$('ul.archiveMonths:first').show();
				$('ul.archivePosts:first').show();
				var elems = $('li.archiveYear a,li.archiveMonth a');
				elems.bind('click',function(e)
				{
					var trg = $(e.target);
					trg.parent().children('ul').toggle();
				});  
			</script>";

		$tags = Tag::getBlogCount();	
		$menu .="<li class='navCategory'>Blog Tags<div class='tagCloudBox'>";

		foreach($tags as $name=>$tag)
			$menu .="<a href='".Util::combinePath($G_HOME,"index.php?pg=blog_tag&amp;tagId=".$tag['tag_id'])."' style='display:inline;'><span style='font-size:1.".($tag["level"])."em;'>".$tag["tag"]."</span></a> ";

		$menu .="</div></li>";
		
		return $menu;
	}
}

?>